package controller;

import Interceptor.AdminCheckInterceptor;
import Interceptor.NavInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import model.User;

/**
 * Created by Administrator on 2018/6/6 0006.
 */
public class UserController extends Controller {
    @Before({NavInterceptor.class, AdminCheckInterceptor.class})
    public void list() {
        int pageNum = getParaToInt("page",1);
        SqlPara user_list = Db.getSqlPara("user_list");
        Page<User> users = User.dao.paginate(pageNum,15,user_list);
        setAttr("users",users);
        renderFreeMarker("list.ftl");
    }
    @Before(NavInterceptor.class)
    public void modify(){
        User user = getSessionAttr("users");
        setAttr("user",user);
        renderFreeMarker("info.ftl");
    }

    public void modifydeal(){
        Integer id = getParaToInt("id");
        String nickname = getPara("nickname");
        String email = getPara("email");
        User user = new User();
        Boolean success = false;
        try{
            user.dao().findById(id).setNickname(nickname).setEmail(email).update();
            success = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        User newUser = User.dao.findById(id);
        setSessionAttr("users",newUser);
        String message = success ? "成功":"用户名或密码错误";
        Kv result = Kv.by("success",success).set("message",message);
        renderJson(result);
    }
}
